<?php

namespace App\Jobs;

use Carbon\Carbon;
use App\Models\BlindBoxOrder;
use App\Models\Order;
use App\Models\User;
use App\Models\Address;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use App\Services\CloseUnpaidProductOrder\CloseUnpaidProductOrder;
use App\Services\MakeProductOrder\MakeProductOrder;
use Illuminate\Support\Facades\Log;

class CloseBoxOrderJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
    protected $order;
    
    public function __construct(BlindBoxOrder $order)
    {
        $this->order = $order;
        $this->delay(Carbon::now()->addMinutes(30));
    }
    
    public function handle()
    {
         Log::info("ceshi");
        $newOrder = Order::query()->where('blind_box_order_id',$this->order->id)->first();

        if($newOrder){

            return false;
        }

       

        $this->user = User::find($this->order->user_id);
        $this->address = Address::find($this->order->address_id);
        $this->items = [
            [
                'sku_id' => $this->order->sku_id,
                'amount' => 1
             ]
        ];
         $order = app(MakeProductOrder::class, [
            'user' => $this->user,
            'address' => $this->address,
            'items' => $this->items,
            'attr' => null,
            'order' => $this->order,
        ])->make();

        $this->order->update(['is_taken' => true]);
    }
}
